'use strict';

function serializeData(data) {
	// If this is not an object, defer to native stringification.
	if (!angular.isObject(data)) {
		return ((data == null) ? "" : data.toString());
	}

	var buffer = [];

	// Serialize each key in the object.
	for (var name in data) {
		if (!data.hasOwnProperty(name)) {
			continue;
		}

		var value = data[name];

		buffer.push(
			encodeURIComponent(name) + "=" + encodeURIComponent((value == null) ? "" : value)
		);
	}

	// Serialize the buffer and clean it up for transportation.
	var source = buffer.join("&").replace(/%20/g, "+");
	return (source);
}
var controllers = angular.module('CourseApp.controllers', []).controller('SidebarController', function($scope, $location, $rootScope, $cookieStore, $state, localStorageService) {

	$scope.student = localStorageService.get('student');

	$scope.$watch(function() {
		return localStorageService.get('student');
	}, function() {
		$scope.student = localStorageService.get('student');
		$rootScope.student = $scope.student;
	}, true);

	$scope.$watch(function() {
		return $rootScope.student;
	}, function() {
		$scope.student = $rootScope.student;
	}, true);

	$scope.logout = function() {
		$rootScope.student = undefined;
		$rootScope.authToken = undefined;
		localStorageService.remove('student');
		localStorageService.remove('authToken');

		$state.go('home');
	};
});

controllers.controller('CourseCtrl', function($scope, $location, $rootScope, $cookieStore, $state, toastr, CourseInfoService, StudentCourseService, TeacherService) {
	if (!$rootScope.student) {
		toastr.info('登录过期，请重新登录');

		$state.go('home');
	}

	$scope.$watch(function() {
		return $rootScope.coursedetaillist;
	}, function() {
		$scope.coursedetaillist = $rootScope.coursedetaillist;
	}, true);

	$scope.$watch(function() {
		return $rootScope.studentcourselist;
	}, function() {
		$scope.studentcourselist = $rootScope.studentcourselist;
	}, true);

	// 获得课程列表
	StudentCourseService.query({}, function(studentcourselist) {
		console.log(studentcourselist);
		// {scId: 1, stuId: 7, couId: 1, locCount: 0, grade: 0}
		// $rootScope.studnetcourselist = courselist;
		$rootScope.coursedetaillist = [];
		$rootScope.studentcourselist = studentcourselist;
		// CourseInfoService.get({couId:1},
		// 	function (data) {
		// 		console.log(data);
		// 	});
		for (var i = 0; i < studentcourselist.length; i++) {
			var temp = studentcourselist[i];
			// console.log(temp);
			CourseInfoService.get({
				couId: temp.couId
			}, function(course) {
				TeacherService.get({
					teaId: course.teaId
				}, function(teacher) {
					course.teacher = teacher;
					console.log(teacher);
					$rootScope.coursedetaillist.push(course);
				});
				// console.log($rootScope.coursedetaillist);
			});
		}
	});
});

controllers.controller('CourseDetailCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, StudentCourseService, TeacherService, NoticeInfoService) {

	// 绑定变量
	$scope.$watch(function() {
		return $scope.course;
	}, function() {
		$rootScope.curcourse = $scope.course;
	}, true);

	StudentCourseService.getInfo({
		scId: $stateParams.scId
	}, function(sc) {
		$scope.sc = sc;
		CourseInfoService.get({
			couId: sc.couId
		}, function(course) {

			$scope.course = course;
			// 获得老师
			TeacherService.get({
				teaId: course.teaId
			}, function(teacher) {
				$scope.teacher = teacher;
			});

			// 获得第一条通知
			NoticeInfoService.getFirstNotice({
				couId: sc.couId
			}, function(notice) {
				// console.log(notice);
				if (notice.notId != undefined) {
					$scope.notice = notice;
				} else {
					$scope.notice = undefined;
				}
			});
		});
	});
});

controllers.controller('CourseNoticeCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, NoticeInfoService) {
	if (!$rootScope.student) {
		toastr.info('登录过期，请重新登录');

		$state.go('home');
	}

	// 获得通知
	NoticeInfoService.getAll({
		couId: $stateParams.couId
	}, function(notices) {
		// console.log(notice);
		if (notices[0] != undefined) {
			$scope.notices = notices;
		} else {
			$scope.notices = undefined;
		}
		console.log(notices);
	});
});

controllers.controller('QuestionAddCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, QuestionInfoService) {
	if (!$rootScope.student) {
		toastr.info('登录过期，请重新登录');

		$state.go('home');
	}

	$scope.addQuestion = function() {
		// body...
		CourseInfoService.getCourseByName({
			couName: $scope.majorName
		}, function(data) {
			if (data.couId == undefined) {
				toastr.warning('课程不存在');
				return;
			}

			$scope.question.couId = data.couId;
			QuestionInfoService.save($scope.question, function(data) {
				if (data.success) {
					toastr.success(data.message);
					$state.go('question.detail', {
						'queId': data.object.queId
					});
				} else {
					toastr.warning(data.message);
				}
			});
		})
	};
});

controllers.controller('QuestionDetailCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, QuestionInfoService, AnswerInfoService) {
	$scope.student = $rootScope.student;
	// $scope.queId = $stateParams.queId;
	QuestionInfoService.get({
		queId: $stateParams.queId
	}, function(question) {
		console.log(question);
		$scope.question = question;
	});

	AnswerInfoService.getAnswersByQueId({
		queId: $stateParams.queId
	}, function(answers) {
		console.log(answers);
		$scope.answers = answers;
	});
});

controllers.controller('AnswerAddCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, QuestionInfoService, AnswerInfoService) {
	$scope.student = $rootScope.student;

	QuestionInfoService.get({
		queId: $stateParams.queId
	}, function(question) {
		console.log(question);
		$scope.question = question;
	});

	$scope.addanswer = function() {
		$scope.answer.queId = $stateParams.queId;
		AnswerInfoService.save($scope.answer, function(data) {
			console.log(data);
			if (data.success) {
				toastr.success(data.message);
				$state.go('question.detail.answer', {
					queId: $stateParams.queId,
					ansId: data.object.answerInfo.ansId
				});
			} else {
				toastr.warning(data.message);
			}
		});
	};
});

controllers.controller('QuestionEditCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, CourseInfoService, QuestionInfoService) {

	QuestionInfoService.get({
		queId: $stateParams.queId
	}, function(question) {
		console.log(question);
		$scope.question = question;
	});

	$scope.submitquestion = function() {
		QuestionInfoService.save($scope.question.questionInfo, function(data) {
			if (data.success) {
				toastr.success(data.message);
				$state.go('question.detail', {
					'queId': data.object.queId
				});
			} else {
				toastr.warning(data.message);
			}
		});
	};
});

controllers.controller('AnswerDetailCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, $timeout, toastr, AnswerInfoService, QuestionInfoService, localStorageService) {
	$scope.student = $rootScope.student;
	if (localStorageService.get('phraselist') == undefined) {
		localStorageService.set('phraselist', {});
	}
	$scope.isPhrase = false;

	QuestionInfoService.get({
		queId: $stateParams.queId
	}, function(question) {
		console.log(question);
		$scope.question = question;
	});

	AnswerInfoService.get({
		ansId: $stateParams.ansId
	}, function(answer) {
		console.log(answer);
		$scope.answer = answer;
		var phraselist = localStorageService.get('phraselist');
		for (var i in phraselist[$scope.student.stuId]) {
			if (phraselist[$scope.student.stuId][i] == $stateParams.ansId) {
				$scope.isPhrase = true;
				break;
			}
		}

		$scope.phrase = function() {
			if($scope.isLimit){
				return;
			}
			$scope.isLimit = true; // 限制乱点

			if ($scope.isPhrase) {
				// 取消赞
				AnswerInfoService.disPhraseAnswer({
					ansId: $stateParams.ansId
				}, function(data) {
					if (data.success) {
						$timeout(function(argument) {
							$scope.isLimit = false;
						}, 3000);
						$scope.isPhrase = false;
						$scope.answer.answerInfo = data.object;

						// 从cookie移除
						var phraselist = localStorageService.get('phraselist');
						if (phraselist[$scope.student.stuId] != undefined) {
							phraselist[$scope.student.stuId].splice(phraselist[$scope.student.stuId].indexOf($scope.answer.answerInfo.ansId), 1);
							localStorageService.set('phraselist', phraselist);
						}
					}
				});
				return;
			}

			AnswerInfoService.phraseAnswer({
				ansId: $stateParams.ansId
			}, function(data) {
				if (data.success) {
					$scope.isPhrase = true;
					$timeout(function(argument) {
						$scope.isLimit = false;
					}, 3000);

					$scope.answer.answerInfo = data.object;

					// 添加到cookie
					var phraselist = localStorageService.get('phraselist');
					if (phraselist[$scope.student.stuId] == undefined) {
						phraselist[$scope.student.stuId] = [];
					}
					phraselist[$scope.student.stuId].push($scope.answer.answerInfo.ansId);
					localStorageService.set('phraselist', phraselist);
				} else {
					toastr.warning(data.message);
				}
			});
		};
	});
});

controllers.controller('AnswerEditCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, AnswerInfoService, QuestionInfoService) {
	$scope.student = $rootScope.student;

	QuestionInfoService.get({
		queId: $stateParams.queId
	}, function(question) {
		console.log(question);
		$scope.question = question;
	});

	AnswerInfoService.get({
		ansId: $stateParams.ansId
	}, function(answer) {
		console.log(answer);
		$scope.answer = answer;
	});

	$scope.editanswer = function(data) {
		$scope.answer.answerInfo.queId = $stateParams.queId;
		AnswerInfoService.save($scope.answer.answerInfo, function(data) {
			console.log(data);
			if (data.success) {
				toastr.success(data.message);
				$state.go('question.detail.answer', {
					queId: $stateParams.queId,
					ansId: data.object.answerInfo.ansId
				});
			} else {
				toastr.warning(data.message);
			}
		});
	};
});

controllers.controller('QuestionListCtrl', function($scope, $location, $rootScope, $cookieStore, $state, $stateParams, toastr, AnswerInfoService, QuestionInfoService, CourseInfoService) {
	$scope.student = $rootScope.student;

	if ($stateParams.couId != 0) {
		CourseInfoService.get({
			couId: $stateParams.couId
		}, function(course) {
			$scope.course = course;
			// console.log($rootScope.coursedetaillist);
		});
		// 专业的
		QuestionInfoService.getQuestionByCouIdOrderByTime({
			couId: $stateParams.couId,
			pageNum: 0
		}, function(newQuestionBlocks) {
			console.log(newQuestionBlocks);
			$scope.newQuestionBlocks = newQuestionBlocks;
		});

		QuestionInfoService.getQuestionByCouIdOrderByGood({
			couId: $stateParams.couId,
			pageNum: 0
		}, function(goodQuestionBlocks) {
			console.log(goodQuestionBlocks);
			$scope.goodQuestionBlocks = goodQuestionBlocks;
		});
	} else {
		//天大全部
		QuestionInfoService.getAllOrderByTime({
			couId: $stateParams.couId,
			pageNum: 0
		}, function(newQuestionBlocks) {
			console.log(newQuestionBlocks);
			$scope.newQuestionBlocks = newQuestionBlocks;
		});

		QuestionInfoService.getAllOrderByGood({
			couId: $stateParams.couId,
			pageNum: 0
		}, function(goodQuestionBlocks) {
			console.log(goodQuestionBlocks);
			$scope.goodQuestionBlocks = goodQuestionBlocks;
		});
	}
});